﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Midgard
{
    class ProjectileManager
    {
        List<Projectile> projectiles;
        List<Enemy> enemiesHit;
        //public Enemy enemyHit;
        public bool playerHitThisInstant;
        public List<Enemy> EnemiesHit
        {
            get { return enemiesHit; }
        }

        public List<Projectile> Projectiles
        {
            get { return projectiles; }
        }

        public ProjectileManager(Rectangle ViewportRectangle)
        {
            projectiles = new List<Projectile>();
            enemiesHit = new List<Enemy>();
            
        }
        public void AddProjectiles(List<Projectile> inProjs)
        {
            foreach (Projectile p in inProjs)
            {
                projectiles.Add(p);
            }
        }
        public void AddProjectile(Projectile p)
        {
            projectiles.Add(p);
        }
        public void Update(GameTime gameTime)
        {
            
            enemiesHit.Clear();
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                projectiles[i].Update();
                //Console.WriteLine(projectiles[i].position.X + ", " + projectiles[i].position.Y);
                if (projectiles[i].active == false)
                {
                    projectiles.RemoveAt(i);
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < projectiles.Count; i++)
            {
                projectiles[i].Draw(spriteBatch);
            }
        }
        public void CollisionDetection(Player player)
        {
            playerHitThisInstant = false;
            Rectangle rect = player.boundRect;
            foreach (Projectile p in projectiles)
            {
                if (p.ProjFaction == Projectile.Faction.enemy)
                {
                    if (p.position.Y + p.Height > rect.Top)
                    {
                        if (p.position.X + p.Width > rect.Left && p.position.X < rect.Right)
                        {
                            player.Hit(p.damage);
                            p.active = false;
                            playerHitThisInstant = true;

                        }
                    }
                }
            }
        }
        public void CollisionDetection(List<Enemy> enemies)
        {
            //Rectangle rect;
            foreach (Projectile p in projectiles)
            {
                if (p.ProjFaction == Projectile.Faction.player)
                {
                    foreach (Enemy e in enemies)
                    {
                        if (e.Bounds.Intersects(p.boundRect))
                        {
                            e.hit(p.damage);
                            p.active = false;
                            if (e.Type == EnemyType.THOR)
                            {
                                enemiesHit.Add(e);
                            }
                        }
                    }
                }
            }
        }
        public void ClearAll()
        {
            projectiles.Clear();
        }
    }

}
